package com.easy.common.util;

import org.springframework.util.DigestUtils;

public class PasswordDecode {
    private static int[] char_arr={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
    /**
     * 密码加密 随机盐值 随机次数
     * @param pass 原文密码
     * @param decode 加密后密码
     * @return
     */
    public static String encryption(String pass, String decode) {
        //加密次数
        int index_count=(int)(Math.random()*char_arr.length);
        //加密盐值
        int index_salt=(int)(Math.random()*char_arr.length);
        String result=pass;
        for(int i=0;i<char_arr[index_count];i++){
            result+=(char)char_arr[index_salt];
            result= DigestUtils.md5DigestAsHex(result.getBytes());
        }
        result=(char)char_arr[index_count]+result+char_arr[index_salt];
        return result;
    }
    public static boolean decode(String pass,String decode) {
        char char_count=decode.charAt(0);
        char char_salt=decode.charAt(decode.length()-1);
        for (int i=0;i<char_count;i++){
            pass=pass+char_salt;
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        return pass.equals(char_count+pass+char_salt);
    }
}
